# -*- mode: python -*-
###
#
# (c) 2000-2007 Wijnand Modderman <wijnand at tehmaze dot com>
# All rights reserved. See docs/LICENSE
#
###

__revision__ = '$Hg: .skell,v 979d136275fb Sun Jan 14 18:37:34 2007 +0100 $'

# DDB imports
from ddb.lang import _
from ddb.tools import syntax
from ddb.access import access

# DDB exports
EXPORT=['Enable']

class Enable:
    @access('nm', 'nm')
    def enable(self, event):
        """<[command]>

        Enables <command> on this transport."""
        if not event.args:
            return event.reply(syntax())
        disabled = event.register.get('disabled', *event.context)
        if not disabled or disabled == [None]:
            disabled = []
        if not event.args in disabled:
            return event.reply(_('The command "%s" was not disabled.' % event.args))
        else:
            disabled.remove(event.args)
            event.register.set('disabled', disabled, *event.context)
            event.reply(_('The command "%s" is now enabled.' % event.args))

    @access('nm', 'nm')
    def disable(self, event):
        """<[command]>

        Disables <command> on this transport."""
        if not event.args:
            return event.reply(syntax())
        disabled = event.register.get('disabled', *event.context)
        if not disabled or disabled == [None]:
            disabled = []
        if event.args in disabled:
            return event.reply(_('The command "%s" was already disabled.' % event.args))
        else:
            disabled.append(event.args)
            event.register.set('disabled', disabled, *event.context)
            event.reply(_('The command "%s" is now disabled.' % event.args))

#
# This part takes care of actually disabling a command (interrupting the
# hook chain with event.stopped, see ddb/modules.py#def hook)
#
    
class Enabler:
    def _onCommand(self, event):
        disabled = event.register.get('disabled', *event.context)
        if disabled and event.command in disabled:
            event.stopped = True

_enabler = Enabler()
bind = (
    ['command', _enabler._onCommand, 199],
    )

# vim:ts=4:sw=4:et:
